package com.example.lenovo.demod;

import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView android_ave, android_max, android_min;
    private TextView english_ave, english_max, english_min;
    private TextView Total_ave, Total_max, Total_min;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //定义组件
        findViews();

        //显示安卓平均分
        ShowAndroidAve();

        //显示安卓最高分
        ShowAndroidMax();

        //显示安卓最低分
        ShowAndroidMin();

        //显示英语平均分
        ShowEnglishAve();

        //显示英语最高分
        ShowEnglishMax();

        //显示英语最低分
        ShowEnglishMin();

        //显示总分平均分
        ShowTotalAve();

        //显示总分最高分
        ShowTotalMax();

        //显示总分最低分
        ShowTotalMin();


    }

    /**
     *   显示总分最低分
     */
    private void ShowTotalMin() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        //everystuTotal 为每个学生的总分  temp暂时存储每个学生的总分  最后得到最低的总分输出
        int everystuTotal = 0, ave = 0,temp=0;
        String name="";
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int android = cursor.getInt(1);
                int english = cursor.getInt(2);
                everystuTotal= english + android;

                // 先假设第一个学生的分数最低  然后让后面的学生的分数与之比较
                if(cursor.isFirst()){
                    temp=everystuTotal;
                }

                if(everystuTotal<temp){
                    temp=everystuTotal;
                    name=cursor.getString(0);
                }

                everystuTotal=0;
            }

            Total_min.setText("总分最低分为"+temp+" "+name);
        }
    }

    /**
     *   显示总分最高分
     */
    private void ShowTotalMax() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        //everystuTotal 为每个学生的总分  temp暂时存储每个学生的总分  最后得到最高总分输出
        int everystuTotal = 0, ave = 0,temp=0;
        String name="";
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int android = cursor.getInt(1);
                int english = cursor.getInt(2);
                everystuTotal= english + android;

                if(everystuTotal>temp){
                    temp=everystuTotal;
                    name=cursor.getString(0);
                }

                everystuTotal=0;
            }

            Total_max.setText("总分最高分为"+temp+" "+name);
        }
    }

    /**
     * 显示总分平均分
     */
    private void ShowTotalAve() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int temp = 0, ave = 0;
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int android = cursor.getInt(1);
                int english = cursor.getInt(2);
                temp+= english + android;
            }

            ave=temp/cursor.getCount();
            Total_ave.setText("总分平均分为" + " " + ave);

        }
    }

    /**
     * 显示英语最低分
     */
    private void ShowEnglishMin() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int temp = 0;
        String name = "";
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int min = cursor.getInt(2);

                if (cursor.isFirst()) {
                    temp = min;
                }

                if (min < temp) {
                    temp = min;
                    name = cursor.getString(0);
                }
            }

            english_min.setText("英语最低分" + " " + temp + " " + name);

        }
    }

    /**
     * 显示英语最高分
     */
    private void ShowEnglishMax() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int temp = 0;
        String name = "";
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int max = cursor.getInt(2);
                if (max > temp) {
                    temp = max;
                    name = cursor.getString(0);
                }
            }

            english_max.setText("英语最高分" + " " + temp + " " + name);

        }
    }


    /**
     * 显示英语平均分
     */
    private void ShowEnglishAve() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int sum = 0, ave = 0;
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int english = cursor.getInt(2);
                sum += english;
            }

            ave = sum / cursor.getCount();
            english_ave.setText("英语平均分" + " " + ave);

        }
    }

    /**
     * 显示安卓最低分
     */
    private void ShowAndroidMin() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int temp = 0;
        String name = "";
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int min = cursor.getInt(1);

                if (cursor.isFirst()) {
                    temp = min;
                }

                if (min < temp) {
                    temp = min;
                    name = cursor.getString(0);
                }
            }

            android_min.setText("安卓最低分" + " " + temp + " " + name);

        }
    }

    /**
     * 显示安卓最高分
     */
    private void ShowAndroidMax() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int temp = 0;
        String name = "";
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int max = cursor.getInt(1);
                if (max > temp) {
                    temp = max;
                    name = cursor.getString(0);
                }
            }

            android_max.setText("安卓最高分" + " " + temp + " " + name);

        }

    }

    /**
     * 显示安卓平均分  读取数据库中学生的安卓成绩
     */
    private void ShowAndroidAve() {
        Uri uri = Uri.parse("content://cn.gdcp.djy/score");
        int sum = 0, ave = 0;
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int android = cursor.getInt(1);
                sum += android;
            }

            ave = sum / cursor.getCount();
            android_ave.setText("安卓平均分" + " " + ave);

        }

    }

    /**
     * 定义组件
     */
    private void findViews() {
        android_ave = findViewById(R.id.android_ave);
        android_max = findViewById(R.id.android_max);
        android_min = findViewById(R.id.android_min);
        english_ave = findViewById(R.id.english_ave);
        english_max = findViewById(R.id.english_max);
        english_min = findViewById(R.id.english_min);
        Total_ave = findViewById(R.id.Total_ave);
        Total_max = findViewById(R.id.Total_max);
        Total_min = findViewById(R.id.Total_min);

    }
}
